package poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class Evaluate {
	
	public final static int HN_STRAIGHT_FLUSH 	= 8;
	public final static int HN_FOUR 			= 7;
	public final static int HN_FULL 			= 6;
	public final static int HN_FLUSH 			= 5;
	public final static int HN_STRAIGHT 		= 4;
	public final static int HN_THREE			= 3;
	public final static int HN_TWO_PAIR 		= 2;
	public final static int HN_ONE_PAIR 		= 1;
	public final static int HN_HIGH_CARD		= 0;

	public static class CardComparator implements Comparator<Card> {
	    @Override
	    public int compare(Card c1, Card c2) {
	        return c1.getFigure()-c2.getFigure();
	    }
	}
	
	public static HandScore getHandScore(Player p)
	{
		
		List<Card> cards = new ArrayList<Card>(p.getCards());
		Collections.sort(cards, new CardComparator());
		
		int fig[] = new int[5];	//figury
		int sui[] = new int[5];	//kolory
		
		for(int i = 0; i<5; i++)
		{
			fig[i] = cards.get(i).getFigure();
			sui[i] = cards.get(i).getSuit();
		}
		
		
		//POKER - POCZATEK
		
		int s;
		int t;
		int i;
		s = sui[0];
		t = fig[0];
		for(i = 1; i<5; i++)
		{
			if(s != sui[i]) break;
			if((t+1) != fig[i]) break;
			t +=1;
		}
		
		if(i == 5)
			return new HandScore(p,HN_STRAIGHT_FLUSH,t);
		
		//POKER - KONIEC
		
		//KARETA - POCZATEK
		t = fig[0];
		for(i = 1; i<4; i++)
			if(t != fig[i]) break;
		
		if(i == 4)
			return new HandScore(p,HN_FOUR,t);

		t = fig[1];
		for(i = 2; i<5; i++)
			if(t != fig[i]) break;
		
		if(i == 5)
			return new HandScore(p,HN_FOUR,t);
		
		//KARETA - KONIEC
		
		//FULL - POCZATEK
		if( 	(fig[0] == fig[1] 
				&& fig[0] == fig[2] )
			&&
			(fig[3] == fig[4])
		)
			return new HandScore(p,HN_FULL,fig[0]);
		
		if( 	(fig[2] == fig[3] 
				&& fig[2] == fig[4] )
			&&
			(fig[0] == fig[1])
		)
			return new HandScore(p,HN_FULL,fig[2]);
		
		//FUL - KONIEC
		
		//KOLOR - POCZATEK
		s = sui[0];
		for(i = 1; i<5; i++)
			if(s != sui[i]) break;

		if(i == 5)
			return new HandScore(p,HN_FLUSH,fig);
		
		//KOLOR - KONIEC
		
		//STRIT - POCZATEK
		t = fig[0];
		for(i = 1; i<5; i++)
		{
			if((t+1) != fig[i]) break;
			t +=1;
		}
		
		if(i == 5)
			return new HandScore(p,HN_STRAIGHT,fig[4]);
		
		//STRIT - KONIEC
		
		//TROJKA - POCZATEK
		if(fig[0] == fig[1]
			&& fig[0] == fig[2] )
			return new HandScore(p,HN_THREE,fig[0]);
		
		if(fig[1] == fig[2] 
			&& fig[1] == fig[3] )
			return new HandScore(p,HN_THREE,fig[1]);
		
		if(fig[2] == fig[3] 
			&& fig[2] == fig[4] )
			return new HandScore(p,HN_THREE,fig[2]);
		
		//TROJKA - KONIEC
		
		//DWIE PARY - POCZATEK
		if(fig[0] == fig[1])
		{
			if(fig[2] == fig[3])
				return new HandScore(p,HN_TWO_PAIR,fig[2],fig[0],fig[4]);
			if(fig[3] == fig[4])
				return new HandScore(p,HN_TWO_PAIR,fig[3],fig[0],fig[2]);
		}
		
		if((fig[1] == fig[2])&&(fig[3] == fig[4]))
			return new HandScore(p,HN_TWO_PAIR,fig[3],fig[1],fig[0]);


		//DWIE PARY - KONIEC
		
		//PARA - POCZATEK
		if(fig[0] == fig[1])
		{
			int[] l = {fig[2], fig[3], fig[4]};
			return new HandScore(p,HN_ONE_PAIR,fig[0],l);
		}
		if(fig[1] == fig[2])
		{
			int[] l = {fig[0], fig[3], fig[4]};
			return new HandScore(p,HN_ONE_PAIR,fig[1],l);
		}
		if(fig[2] == fig[3])
		{
			int[] l = {fig[0], fig[1], fig[4]};
			return new HandScore(p,HN_ONE_PAIR,fig[2],l);
		}
		if(fig[3] == fig[4])
		{
			int[] l = {fig[0], fig[1], fig[2]};
			return new HandScore(p,HN_ONE_PAIR,fig[3],l);
		}
		
		//PARA - KONIEC
		
		//NAJWYZSZA KARTA
		return new HandScore(p,HN_HIGH_CARD, fig );
		
		
	}
	
	public static List<Card> getExchangeProposition (Player p)
	{
		
		List<Card> cards = new ArrayList<Card>(p.getCards());
		Collections.sort(cards, new CardComparator());
		
		int fig[] = new int[5];	//figury
		int sui[] = new int[5];	//kolory
		
		for(int i = 0; i<5; i++)
		{
			fig[i] = cards.get(i).getFigure();
			sui[i] = cards.get(i).getSuit();
		}
			
		//POKER - POCZATEK
		
		int s;
		int t;
		int i;
		s = sui[0];
		t = fig[0];
		for(i = 1; i<5; i++)
		{
			if(s != sui[i]) break;
			if((t+1) != fig[i]) break;
			t +=1;
		}
		
		if(i == 5)
			return new ArrayList<Card>(0);
		
		//POKER - KONIEC
		
		//KARETA - POCZATEK
		t = fig[0];
		for(i = 1; i<4; i++)
			if(t != fig[i]) break;
		
		if(i == 4)
			return new ArrayList<Card>(0);

		t = fig[1];
		for(i = 2; i<5; i++)
			if(t != fig[i]) break;
		
		if(i == 5)
			return new ArrayList<Card>(0);
		
		//KARETA - KONIEC
		
		//FULL - POCZATEK
		if( 	(fig[0] == fig[1] 
				&& fig[0] == fig[2] )
			&&
			(fig[3] == fig[4])
		)
			return new ArrayList<Card>(0);
		
		if( 	(fig[2] == fig[3] 
				&& fig[2] == fig[4] )
			&&
			(fig[0] == fig[1])
		)
			return new ArrayList<Card>(0);
		
		//FUL - KONIEC
		
		//KOLOR - POCZATEK
		s = sui[0];
		for(i = 1; i<5; i++)
			if(s != sui[i]) break;

		if(i == 5)
			return new ArrayList<Card>(0);
		
		//KOLOR - KONIEC
		
		//STRIT - POCZATEK
		t = fig[0];
		for(i = 1; i<5; i++)
		{
			if((t+1) != fig[i]) break;
			t +=1;
		}
		
		if(i == 5)
			return new ArrayList<Card>(0);
		
		//STRIT - KONIEC
		
		//TROJKA - POCZATEK
		if(fig[0] == fig[1]
			&& fig[0] == fig[2] )
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(3));
			array.add(cards.get(4));
			return array;
		}
		
		if(fig[1] == fig[2] 
			&& fig[1] == fig[3] )
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			array.add(cards.get(4));
			return array;
		}
		
		if(fig[2] == fig[3] 
			&& fig[2] == fig[4] )
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			array.add(cards.get(1));
			return array;
		}
		

		
		//TROJKA - KONIEC
		
		//DWIE PARY - POCZATEK
		if(fig[0] == fig[1])
		{
			if(fig[2] == fig[3])
			{
				List<Card> array = new ArrayList<Card>();
				array.add(cards.get(4));
				return array;
			}
			if(fig[3] == fig[4])
			{
				List<Card> array = new ArrayList<Card>();
				array.add(cards.get(2));
				return array;
			}
		}
		
		if((fig[1] == fig[2])&&(fig[3] == fig[4]))
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			return array;
		}


		//DWIE PARY - KONIEC
		
		//PARA - POCZATEK
		if(fig[0] == fig[1])
		{
				List<Card> array = new ArrayList<Card>();
				array.add(cards.get(2));
				array.add(cards.get(3));
				array.add(cards.get(4));
				return array;
		}
		if(fig[1] == fig[2])
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			array.add(cards.get(2));
			array.add(cards.get(3));
			return array;
		}
		if(fig[2] == fig[3])
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			array.add(cards.get(1));
			array.add(cards.get(4));
			return array;
	}
		if(fig[3] == fig[4])
		{
			List<Card> array = new ArrayList<Card>();
			array.add(cards.get(0));
			array.add(cards.get(1));
			array.add(cards.get(2));
			return array;
	}
		
		//PARA - KONIEC
		
		//NAJWYZSZA KARTA
		cards.remove(4);
		return cards;
		
		
	}
	
	
	public static List<HandScore> getScores(Player[] players)
	{
		int l = players.length;
		List<HandScore> scores = new ArrayList<HandScore>();
		
		for(int i = 0; i <l; i++)
			scores.add(getHandScore(players[i]));
		
		Collections.sort(scores);
		
		return scores;
		
	}

}
